package cn.distant.tank;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;

/**
 * @author distantstar
 * @version 1.0
 * @date 2021
 * @description cn.distant.tank
 * @since JDK1.8
 */
public class Tank {

    private int x, y;
    private int width, height;
    private Dir dir;
    private final int SPEED = Integer.parseInt((String)PropertiesMgr.getKey("tankSpeed"));
    private boolean moving = true;
    private TankFrame tf;
    private boolean live = true;
    private boolean main = true;
    private Group group;
    private final Random random = new Random();
    private BufferedImage bufferedImage;
    private Rectangle rect = new Rectangle();

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public boolean isMain() {
        return main;
    }

    public void setMain(boolean main) {
        this.main = main;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public Rectangle getRect() {
        return rect;
    }

    public void setRect(Rectangle rect) {
        this.rect = rect;
    }

    public Tank(int x, int y, Dir dir, TankFrame tf, Group group) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tf = tf;
        this.group = group;

        this.rect.x = this.x;
        this.rect.y = this.y;
        this.rect.width = width;
        this.rect.height = height;
    }

    public void paint(Graphics g) {
        if (!live && !main) {
            tf.tanks.remove(this);
        }

        // 根据方向设置不同的坦克图片
        switch (dir) {
            case RIGHT:
                bufferedImage = this.group == Group.GOOD ? ResourceMgr.gTankR : ResourceMgr.bTankR;
                width = bufferedImage.getWidth();
                height = bufferedImage.getHeight();
                g.drawImage(bufferedImage, x, y, null);
                break;
            case LEFT:
                bufferedImage = this.group == Group.GOOD ? ResourceMgr.gTankL : ResourceMgr.bTankL;
                width = bufferedImage.getWidth();
                height = bufferedImage.getHeight();
                g.drawImage(bufferedImage, x, y, null);
                break;
            case UP:
                bufferedImage = this.group == Group.GOOD ? ResourceMgr.gTankU : ResourceMgr.bTankU;
                width = bufferedImage.getWidth();
                height = bufferedImage.getHeight();
                g.drawImage(bufferedImage, x, y, null);
                break;
            case DOWN:
                bufferedImage = this.group == Group.GOOD ? ResourceMgr.gTankD : ResourceMgr.bTankD;
                width = bufferedImage.getWidth();
                height = bufferedImage.getHeight();
                g.drawImage(bufferedImage, x, y, null);
                break;
            default:
                break;
        }


        // 调用移动函数
        move();


    }

    private void boundsCheck() {
        if (this.x < 2) {
            x = 2;
        }
        if (this.y < 28) {
            y = 28;
        }
        if (this.x > TankFrame.GAME_WIDTH - width - 2) {
            x = TankFrame.GAME_WIDTH - width - 2;
        }
        if (this.y > TankFrame.GAME_HEIGHT - height - 2) {
            y = TankFrame.GAME_HEIGHT - height - 2;
        }
    }

    /**
     * 移动
     */
    private void move() {

        if (moving) {
            if (this.group == Group.GOOD) {
                new Thread(()->new Audio("audio/tank_move.wav").play()).start();
            }

            // 让坦克可以根据按键移动
            switch (dir) {
                case RIGHT:
                    x += SPEED;
                    break;
                case LEFT:
                    x -= SPEED;
                    break;
                case UP:
                    y -= SPEED;
                    break;
                case DOWN:
                    y += SPEED;
                    break;
                default:
                    break;
            }

            if (this.group == Group.BAD && random.nextInt(100) > 95) {
                this.fire();
            }

            if(this.group == Group.BAD && random.nextInt(100) > 95) {
                randomDir();
            }

            // 边界检测
            boundsCheck();

            rect.x = x;
            rect.y = y;
        }
    }

    /**
     * 开火
     */
    public void fire() {
        if (this.group == Group.GOOD) {
            new Thread(()->new Audio("audio/tank_fire.wav").play()).start();
        }

        // 判断朝哪个方向开火
        switch (dir) {
            case RIGHT:
                tf.bullets.add(new Bullet(this.x + ((this.width - Bullet.width) >> 1) + 33,
                        this.y + ((this.height - Bullet.width) >> 1),
                        this.dir, this.tf, this.group));
                break;
            case LEFT:
                tf.bullets.add(new Bullet(this.x + ((this.width - Bullet.width) >> 1) - 33,
                        this.y + ((this.height - Bullet.width) >> 1) + 2,
                        this.dir, this.tf, this.group));
                break;
            case UP:
                tf.bullets.add(new Bullet(this.x + ((this.width - Bullet.width) >> 1),
                        this.y + ((this.height - Bullet.width) >> 1) - 30,
                        this.dir, this.tf, this.group));
                break;
            case DOWN:
                tf.bullets.add(new Bullet(this.x + ((this.width - Bullet.width) >> 1),
                        this.y + ((this.height - Bullet.width) >> 1) + 30,
                        this.dir, this.tf, this.group));
                break;
            default:
                break;
        }
    }

    public void die() {
        this.live = false;
    }

    private void randomDir() {
        this.dir = Dir.values()[random.nextInt(4)];
    }
}
